﻿using System;
using System.Collections.Generic;

namespace Shot.Routing
{
    /// <summary>
    /// Implementats IRouter to act as a generic router for Eventd emissions.
    /// </summary>
    public class Router : IRouter
    {
        /// <summary>
        /// App Instance
        /// </summary>
        public App App { get; set; }

        /// <summary>
        /// Dictionary of route strings and corresponding Actions
        /// </summary>
        public Dictionary<String, Action<AppInstance>> Routes { get; set; }

        /// <summary>
        /// Default route string, which is used when the user requests the app in a generic state
        /// </summary>
        public string DefaultRoute
        {
            get
            {
                return "";
            }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        public Router()
        {
            Routes = new Dictionary<string, Action<AppInstance>>();
        }

        /// <summary>
        /// Handle a route string
        /// </summary>
        /// <param name="route"></param>
        /// <param name="instance"></param>
        public void Route(string route, AppInstance instance)
        {
            if (IsRouted(route))
            {
                Routes[route].Invoke(instance);
            }
        }

        /// <summary>
        /// Check if a route is mapped
        /// </summary>
        /// <param name="route"></param>
        /// <returns></returns>
        public bool IsRouted(string route)
        {
            return Routes.ContainsKey(route);
        }

        /// <summary>
        /// Add a route
        /// </summary>
        /// <param name="routeName"></param>
        /// <param name="routeAction"></param>
        public void Add(string routeName, Action<AppInstance> routeAction)
        {
            Routes.Add(routeName, routeAction);
        }

        /// <summary>
        /// Remove a route
        /// </summary>
        /// <param name="routeName"></param>
        public void Remove(string routeName)
        {
            Routes.Remove(routeName);
        }
    }
}
